'''
Created on 21 Nov 2009

@author: ralf
'''

from window import PbWindow
from treemodel import TreeModel, EnumItemCreator, CustomItemDelegate
from PyQt4.QtGui import QApplication, QItemEditorFactory
from PyQt4.QtCore import QVariant
from PyQt4 import QtCore, QtGui

def open_file():
    filename = QtGui.QFileDialog.getOpenFileName(None, "Binary File")
    if filename:
        global model
        f = open(str(filename), 'rb')
        data = f.read()
        f.close()
        model.set_data(data)
     
def load_descriptors():
    filename = QtGui.QFileDialog.getOpenFileName(None, "Protobuf File", "", "Python pb files (*_pb2.py);;All files (*)")
    if filename:
        global model
        import loader
        descriptors = loader.load_descriptors(str(filename))
        model.set_descriptors(descriptors)
     
if __name__ == '__main__':
    import sys
    
    app = QApplication(sys.argv)
    window = PbWindow()
    global model
    model = TreeModel()
    window.treeView.setModel(model)
    window.treeView.setItemDelegate(CustomItemDelegate())
    window.actionOpen.triggered.connect(open_file)
    window.actionLoadDescriptors.triggered.connect(load_descriptors)
        
    window.show()
    result = app.exec_()
    sys.exit(result)
    